package com.mingtu.guards.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.ActivityUtil;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.MyLogUtil;
import com.mingtu.common.utils.MyUtills;
import com.mingtu.common.utils.UIUtils;
import com.mingtu.common.view.ContainsEmojiEditText;
import com.mingtu.guards.R;
import com.mingtu.guards.base.MyBaseActivity;
import com.mingtu.guards.utils.MyConstant;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;

public class ReplyPostActivity extends MyBaseActivity {

    @BindView(R.id.edit_describe)
    ContainsEmojiEditText editDescribe;
    @BindView(R.id.tv_total_text_count)
    TextView tvTotalTextCount;
    @BindView(R.id.but_submit)
    Button butSubmit;
    private String speciesId;
    private int textNum = 0;
    private int totalTextCount = 200;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_reply_post;
    }

    @Override
    protected void initView() {
        setModuleTitle("回复帖子");
    }

    @Override
    protected void initData() {
        speciesId = getIntent().getStringExtra("speciesId");
        editDescribe.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                textNum -= count;
                tvTotalTextCount.setText((totalTextCount - textNum) + "");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                textNum += count;
                tvTotalTextCount.setText((totalTextCount - textNum) + "");
            }

            @Override
            public void afterTextChanged(Editable s) {
                MyLogUtil.e("MyTest", "afterTextChanged==" + s.toString());
                String str = MyUtills.getEditText(editDescribe);
                editDescribe.setSelection(str.length());
            }
        });
    }

    @Override
    protected void getData() {

    }


    @OnClick({R.id.but_submit})
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.but_submit:
                common();
                break;
        }
    }

    /**
     * 评论
     */
    private void common() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("comment", UIUtils.getEditText(editDescribe));
        hashMap.put("speciesId", speciesId);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_SPECIES_COMMENT)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(this) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            JSONObject jsonObject = JSON.parseObject(result);
                            String msg = jsonObject.getString("msg");
                            if (msg.equals(Constant.RESULT_SUCCESS)) {
                                setResult(200);
                                ActivityUtil.removeActivity(ReplyPostActivity.this);
                            } else {
                                ToastUtils.showLong("回复失败！");
                            }
                        }
                    }
                });
    }
}